package com.base.lib.base.view

import android.view.View
import android.view.ViewStub
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jeremyliao.liveeventbus.LiveEventBus
import com.base.lib.base.mvvm.view.BaseRefreshView
import com.base.lib.base.mvvm.viewmodel.BaseRefreshViewModel
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.base.lib.R
import com.base.lib.base.adapter.BaseSkeletonAdapter
import com.base.lib.base.utils.RvAnimUtils
import com.base.lib.base.utils.SettingUtil
import com.base.lib.base.utils.ThreadUtils
import com.base.lib.base.widget.MyRefreshHeader
import com.base.lib.net.utils.ext.observe

/**
 * Describe:
 * 基于 MVVM 刷新页面
 *
 */
abstract class BaseMvvmListActivity<T, VM : BaseRefreshViewModel<T>, AD : BaseQuickAdapter<T, BaseViewHolder>> :
    BaseMvvmActivity<VM>(),
    BaseRefreshView {

    protected val mAdapter: AD by lazy {
        onBindAdapter().newInstance()
    }

    protected var params: Any? = null

    protected lateinit var mRefreshLayout: SmartRefreshLayout
    protected lateinit var mRecyclerView: RecyclerView
    protected lateinit var mRefreshHeader: MyRefreshHeader
    protected var isRefresh = true

    /**
     * 绑定 ViewModel
     */
    abstract fun onBindAdapter(): Class<AD>

    override fun onBindLayout(): Int = R.layout.base_list_fragment

    override fun initCommonView() {
        super.initCommonView()
        // 绑定组件
        mRefreshLayout = mContentView.findViewById(R.id.swipeRefreshLayout_know_list)
        mRecyclerView = mContentView.findViewById(R.id.recyclerView_know_list)
        mAdapter.recyclerView = mRecyclerView
        mRefreshHeader = mContentView.findViewById(R.id.ch_header_know_list)
        // 是否开启刷新   // 默认不开启
        enableRefresh(true)
        // 是否开启加载更多
        enableLoadMore(true)

        mRefreshLayout.setRefreshHeader(mRefreshHeader)
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mContext)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
        }
        // 下拉刷新
        mRefreshLayout.setOnRefreshListener {
            onRefreshEvent()
        }
        // 上拉加载
        mRefreshLayout.setOnLoadMoreListener {
            onLoadMoreEvent()
        }
        RvAnimUtils.setAnim(mAdapter, SettingUtil.getListAnimal())
        LiveEventBus.get("rv_anim").observe(this, {
            RvAnimUtils.setAnim(mAdapter, it)
        })

        mViewModel.mUIChangeRefreshLiveData.autoRefreshLiveEvent.observe(this) {
            autoLoadData()
        }
        mViewModel.mUIChangeRefreshLiveData.stopRefreshLiveEvent.observe(this) {
            stopRefresh(it)
        }
        mViewModel.mUIChangeRefreshLiveData.stopLoadMoreLiveEvent.observe(this) {
            stopLoadMore(it)
        }
    }

    override fun initViewObservable() {
        observe(mViewModel.mData, ::refreshAdapterData)
    }

    override fun enableRefresh(b: Boolean) {
        mRefreshLayout.setEnableRefresh(b)
    }

    override fun enableLoadMore(b: Boolean) {
        mRefreshLayout.setEnableLoadMore(b)
    }

    override fun enableAutoLoadMore(b: Boolean) {
        mRefreshLayout.setEnableAutoLoadMore(b)
    }

    private var firstLoad = true
    override fun onRefreshEvent() {
        mAdapter.loadMoreModule.isEnableLoadMore = false
        isRefresh = true
        // 为了展示骨架屏
        if (firstLoad) {
            firstLoad = false
            ThreadUtils.runOnUiThread({ onRefreshEvent() }, 1000)
        } else {
            mViewModel.refreshData(params)
        }
    }

    override fun onLoadMoreEvent() {
        isRefresh = false
        mRefreshLayout.finishRefresh()
        mViewModel.loadMore(params)
    }

    open fun refreshAdapterData(list: List<T>) {
        list.also {
            it.let { Article ->
                mAdapter.run {
                    hideInitLoadView()
                    if (isRefresh) {
                        if (mAdapter is BaseSkeletonAdapter<*, *>) (mAdapter as BaseSkeletonAdapter<T, BaseViewHolder>).hideSkeleton()
                        mRefreshLayout.finishRefresh()
                        setList(Article)
                        recyclerView.scrollToPosition(0)
                    } else {
                        addData(Article)
                        mRefreshLayout.finishLoadMore()
                    }
                    if (data.size == 0) showNoDataView()
                    else if (hasEmptyView()) hideNoDataView()
                    if (it.size < mViewModel.pageSize) loadMoreModule.loadMoreEnd(isRefresh)
                    else loadMoreModule.loadMoreComplete()
                }
            }
        }
    }

    override fun onAutoLoadEvent() {
    }

    override fun autoLoadData() {
        mRefreshLayout.autoRefresh()
    }

    override fun stopRefresh(boolean: Boolean) {
        mRefreshLayout.finishRefresh(boolean)
    }

    override fun stopLoadMore(boolean: Boolean) {
        mRefreshLayout.finishLoadMore(boolean)
    }

    override fun requestError(it: Exception?) {
        super.requestError(it)
        mAdapter.loadMoreModule.loadMoreFail()
    }
}
